package 线程池.线程池处理Callable任务;

import java.util.concurrent.*;

public class Test {
    public static void main(String[] args) throws Exception {
        // ExecutorService的常用方法
        // 1. void execute(Runnable command): 执行任务/命令, 没有返回值, 一般用来执行 Runnable任务
        // 2. Future<T> submit(Callable<T> task): 执行任务, 返回未来任务对象获取线程结果, 一般拿来执行 Callable任务
        // 3. void shutdown(): 等任务执行完毕后关闭线程
        // 4. List<Runnable> shutdownNOw(): 立刻关闭,停止正在执行的任务, 并返回队列中未执行的任务

        // 1. 通过ThreadPoolExecutor创建一个线程池对象
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS, new ArrayBlockingQueue<>(4),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        // 2. 使用线程处理Callable任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());

    }
}
